﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Audit_Configuration_Oracle.Controllers;

namespace Audit_Configuration_Oracle.Views
{
    public partial class ACO_New_Rule : Form
    {
        string rule = "";
        List<string> operations = new List<string>();
        string condition1 = "";
        string condition2 = "";

        public ACO_New_Rule()
        {
            InitializeComponent();
        }

        private void rbSentencias_CheckedChanged(object sender, EventArgs e)
        {
            if (rbSentencias.Checked)
            {
                rule = "S";
                cbInsert.Visible = true;
                cbSelect.Visible = true;
                cbUpdate.Visible = true;
                cbDelete.Visible = true;
                cbCreateTable.Visible = false;
                cbCreateSession.Visible = false;
                cbExecuteProcedure.Visible = false;
                cbxObjetivo.Items.Clear();
                cbxObjetivo.Items.AddRange(NewRuleController.GetUserList().ToArray());
                cbxObjetivo.SelectedIndex = 0;
                this.resetOperations();
            }
        }

        private void rbObjetos_CheckedChanged(object sender, EventArgs e)
        {
            if (rbObjetos.Checked)
            {
                rule = "O";
                cbInsert.Visible = true;
                cbSelect.Visible = true;
                cbUpdate.Visible = true;
                cbDelete.Visible = true;
                cbCreateTable.Visible = false;
                cbCreateSession.Visible = false;
                cbExecuteProcedure.Visible = false;
                cbxObjetivo.Items.Clear();
                cbxObjetivo.Items.AddRange(NewRuleController.GetTableList().ToArray());
                cbxObjetivo.SelectedIndex = 0;
                this.resetOperations();
            }
        }

        private void rbPrivilegios_CheckedChanged(object sender, EventArgs e)
        {
            if (rbPrivilegios.Checked)
            {
                rule = "P";
                cbCreateTable.Visible = true;
                cbCreateSession.Visible = true;
                cbExecuteProcedure.Visible = true;
                cbInsert.Visible = false;
                cbSelect.Visible = false;
                cbUpdate.Visible = false;
                cbDelete.Visible = false;
                cbxObjetivo.Items.Clear();
                cbxObjetivo.Items.AddRange(NewRuleController.GetUserList().ToArray());
                cbxObjetivo.SelectedIndex = 0;
                this.resetOperations();
            }

        }

        private void cbInsert_CheckedChanged(object sender, EventArgs e)
        {
            if (cbInsert.Checked)
            {
                operations.Add("INSERT");
            }
            else 
            {
                operations.Remove("INSERT");
            }
        }

        private void cbSelect_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSelect.Checked)
            {
                operations.Add("SELECT");
            }
            else
            {
                operations.Remove("SELECT");
            }
        }

        private void cbDelete_CheckedChanged(object sender, EventArgs e)
        {
            if (cbDelete.Checked)
            {
                operations.Add("DELETE");
            }
            else
            {
                operations.Remove("DELETE");
            }
        }

        private void cbUpdate_CheckedChanged(object sender, EventArgs e)
        {
            if (cbUpdate.Checked)
            {
                operations.Add("UPDATE");
            }
            else
            {
                operations.Remove("UPDATE");
            }
        }

        private void cbExecuteProcedure_CheckedChanged(object sender, EventArgs e)
        {
            if (cbExecuteProcedure.Checked)
            {
                operations.Add("EXECUTE PROCEDURE");
            }
            else
            {
                operations.Remove("EXECUTE PROCEDURE");
            }
        }

        private void cbCreateTable_CheckedChanged(object sender, EventArgs e)
        {
            if (cbCreateTable.Checked)
            {
                operations.Add("CREATE TABLE");
            }
            else
            {
                operations.Remove("CREATE TABLE");
            }
        }

        private void cbCreateSession_CheckedChanged(object sender, EventArgs e)
        {
            if (cbCreateSession.Checked)
            {
                operations.Add("CREATE SESSION");
            }
            else
            {
                operations.Remove("CREATE SESSION");
            }
        }

        private void rbAcceso_CheckedChanged(object sender, EventArgs e)
        {
            if (rbAcceso.Checked)
            {
                condition1 = "ACCESS";
            }
        }

        private void rbSesion_CheckedChanged(object sender, EventArgs e)
        {
            if (rbSesion.Checked)
            {
                condition1 = "SESSION";
            }
        }

        private void rbEmpty1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbEmpty1.Checked)
            {
                condition1 = "";
            }
        }

        private void rbExitosa_CheckedChanged(object sender, EventArgs e)
        {
            if (rbExitosa.Checked)
            {
                condition2 = "WHENEVER SUCCESSFUL";
            }
        }

        private void rbFallida_CheckedChanged(object sender, EventArgs e)
        {
            if (rbFallida.Checked)
            {
                condition2 = "WHENEVER NOT SUCCESSFUL";
            }
        }

        private void rbEmpty2_CheckedChanged(object sender, EventArgs e)
        {
            if (rbEmpty2.Checked)
            {
                condition2 = "";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ACO_Audit_Rules r = null;
            foreach (Form child in this.MdiParent.MdiChildren)
            {
                if (child.Name.Equals("ACO_Audit_Rules"))
                {
                    r = (ACO_Audit_Rules)child;
                }
            }

            //ACO_Audit_Rules r = (ACO_Audit_Rules)this.MdiParent.MdiChildren.;
            bool result = false;
            if (operations.Count > 0)
            {
                if (rule.Equals("S") || rule.Equals("O"))
                {
                    result = NewRuleController.SaveNewRule(rule, operations, cbxObjetivo.Text, condition1, condition2);
                }
                else if (rule.Equals("P"))
                {
                    result = NewRuleController.SaveNewRulePrivilege(rule, operations, cbxObjetivo.Text, condition1, condition2);
                }
                if (result)
                {
                    MessageBox.Show("Transaccion Exitosa", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.resetAllControls();
                }
                else
                {
                    MessageBox.Show("Transaccion Fallida", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar al menos una operación", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (r != null)
            {
                r.reloadAllControls();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void resetOperations()
        {
            operations = new List<string>();
            cbCreateTable.Checked = false;
            cbCreateSession.Checked = false;
            cbExecuteProcedure.Checked = false;
            cbInsert.Checked = false;
            cbSelect.Checked = false;
            cbUpdate.Checked = false;
            cbDelete.Checked = false;
        }

        private void resetAllControls() 
        {
            this.rbSentencias.Select();
            this.resetOperations();
            this.rbEmpty1.Select();
            this.rbEmpty2.Select();
        }

        private void ACO_New_Rule_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            resetAllControls();
            this.Hide();
        }

    }
}

